using System;
using System.Text;
using System.Data;
using System.Collections;
using System.Collections.Generic;

using Framework;

namespace SchemaMonitor
{
    //Table-Row Class (Customisable half)
    public partial class CImageMatching
    {
        #region Constants
        #endregion

        #region Constructors (Public)
        //Default Connection String
        public CImageMatching() : base() {}
        public CImageMatching(Guid mD5, Guid matchingMd5) : base(mD5, matchingMd5) {}
        
        //Alternative Connection String
        public CImageMatching(CDataSrc dataSrc) : base(dataSrc) {}
        public CImageMatching(CDataSrc dataSrc, Guid mD5, Guid matchingMd5) : base(dataSrc, mD5, matchingMd5) {}
        
        //Transactional (shares an open connection)
        protected internal CImageMatching(CDataSrc dataSrc, Guid mD5, Guid matchingMd5, IDbTransaction txOrNull) : base(dataSrc, mD5, matchingMd5, txOrNull) {}        
        #endregion

        #region Default Values
        protected override void InitValues_Custom()
        {
            //Custom default values (e.g. DateCreated column)
            //Member variables (e.g. for child collections)
        }
		#endregion

		#region Default Connection String
		protected override CDataSrc DefaultDataSrc() { return CConfig.Photos; }
		#endregion

		#region Members
		//Foreign Keys

		//Child Collections

		//Xml Data (as high-level objects)

		#endregion

		#region Properties - Relationships
		//Relationships - Foriegn Keys (e.g parent)

		//Relationships - Collections (e.g. children)

		#endregion

		#region Properties - Customisation
		//Derived/ReadOnly (e.g. xml classes, presentation logic)
		#endregion

		#region Save/Delete Overrides
		//Can Override base.Save/Delete (e.g. Cascade deletes, or insert related records)
		#endregion

		#region Custom Database Queries
		//For Stored Procs can use: MakeList (matching schema), or DataSrc.ExecuteDataset (reports etc)
		//For Dynamic sql, can use: SelectSum, SelectDistinct, SelectCount, SelectWhere (inherited methods)
		//                see also: SelectBy[FK], Search and Count (auto-generated sample queries)
		#endregion

		#region Cloning
		public CImageMatching Clone(CDataSrc target, IDbTransaction txOrNull) //, int parentId)
        {
            //Shallow copy: Copies the immediate record, excluding autogenerated Pks
            CImageMatching copy = new CImageMatching(this, target);

            //Deep Copy - Child Entities: Cloned children must reference their cloned parent
            //copy.SampleParentId = parentId;

            copy.Save(txOrNull);

            //Deep Copy - Parent Entities: Cloned parents also clone their child collections
            //this.Children.Clone(target, txOrNull, copy.MD5);

            return copy;
        }
        #endregion

        #region ToXml
        protected override void ToXml_Custom(System.Xml.XmlWriter w)
        {
            //Store(w, "Example", this.Example)
        }
		#endregion



		public static CImageMatching Store(Guid g1, Guid g2, int x, CDataSrc db = null)
		{
			if (null == db) db = CConfig.Photos;

			var im = new CImageMatching(db);
			im.MD5 = g1;
			im.MatchingMd5 = g2;
			im.Score = x;
			im.Save();
			return im;
		}
		public static string Progress<T>(IList<T> list, T item)
		{
			var num = list.IndexOf(item) + 1;
			var tot = list.Count;
			var per = ProgressPercent<T>(list, item);
			return per.ToString("n2") + "%";
		}
		public static double ProgressPercent<T>(IList<T> list, T item)
		{
			var num = list.IndexOf(item) + 1;
			var tot = list.Count;
			return 100.0 * num / tot;
		}
	}
}